package lab03.test13;

import java.sql.*;


public class test13 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        String sqlselect  = "select * from t_students";
        String sqldelete="alter table t_students drop colunm" +
                "where id='5'";
        String sql="update t_students set depratment='信息与工程' where id='3'";
        // String sqlinsert = "insert into t_students(stu_no,real_name,age,genda,depratment)values('1603','小哥哥',18,1,'信息与工程')";


        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC",
                    "root",
                    "123456");//链接到数据库

            statement = connection.createStatement();//插入数据操作
//            statement.execute(sqlinsert);
//            statement.execute(sqldelete);
            statement.execute(sql);

            resultSet = statement.executeQuery(sqlselect);//查询操作
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String stu_no = resultSet.getString(2);
                String name = resultSet.getString(3);
                int age = resultSet.getInt(4);
                int genda = resultSet.getInt(5);
                String dept = resultSet.getString(6);
                System.out.println("id" + id + "stu_no" + stu_no + "age" + age + "genda" + genda + "dept" + dept);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
                //               statement.close();
//                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
